package com.stars.empoly.vmodel

import android.app.Activity
import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.hwangjr.rxbus.RxBus
import com.stars.empoly.activity.AboutActivity
import com.stars.empoly.activity.AddressListActivity
import com.stars.empoly.activity.ReceivedDetailsActivity
import com.stars.empoly.componnent.CpAlipay
import com.stars.empoly.config.ConfigRx
import com.stars.empoly.config.MIntentAction
import com.stars.empoly.model.*
import com.stars.empoly.model.bean.PlatInfo
import com.stars.empoly.utils.net.NetEngine
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import com.trailer.base.utils.B
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch

class PlaceOrderVModel(application: Application) : BViewModel<BRepository>(application) {


    //技师的id
    var technicianId: String? = null
    var name = MutableLiveData<String>()

    //默认地址
    var isShowAddress = MutableLiveData<Boolean>(false)//默认没有
    var addressId: String? = null
    var realName = MutableLiveData<String>()
    var phone = MutableLiveData<String>()
    var detailsAddress = MutableLiveData<String>()

    //车费
    var isShowFee = MutableLiveData<Boolean>(false)
    var fare: String? = "0"
    var fee = MutableLiveData<FareBean>()

    //优惠卷
    var useMinPrice:String?="0"
    var couponFee: String? = "0"
    var couponId: String? = null
    var couponTips=MutableLiveData<String>()

    //服务的费用
    var serverFee: String? = "0"
    var zFee = MutableLiveData<String>()//总费用 =服务费+车费-优惠卷

    //备注
    var remak: String? = ""

    //支付方式
    var isYue = MutableLiveData<Boolean>(false)
    var isWx = MutableLiveData<Boolean>(true)
    var isZhi = MutableLiveData<Boolean>(false)

    var payType: String? = "2"//1余额支付 2微信 3支付宝

    //技师的时间
    var choseTime: String? = ""
    var dateList = MutableLiveData<List<DateBean>>()

    //再来一单 该技师的服务
    var bean=MutableLiveData<ServiceList>()

    var orderBean= MutableLiveData<UseOrderDetail>()

    var isGetAddress=false

    //支付
    var orderId:String?=null



    fun setPayType(type: Int?) {

        when (type) {
            1 -> {
                payType = "1"
                isYue.value = true
                isWx.value = false
                isZhi.value = false
            }
            2 -> {
                payType = "2"
                isYue.value = false
                isWx.value = true
                isZhi.value = false
            }
            3 -> {
                payType = "3"
                isYue.value = false
                isWx.value = false
                isZhi.value = true
            }
        }

    }


    fun setSum() {
        //先判读有没有优惠卷
        if(couponFee.isNullOrEmpty()) {
            var s = serverFee!!.toDouble() + fare!!.toDouble() - couponFee!!.toDouble()
            zFee.value = "" + s
        }else{
           if(useMinPrice!!.toDouble()<=serverFee!!.toDouble()){
               var s = serverFee!!.toDouble() + fare!!.toDouble() - couponFee!!.toDouble()
               zFee.value = "" + s
           }else{
               useMinPrice="0"
               couponFee="0"
               couponId="0"
               couponTips.value=null
               var s = serverFee!!.toDouble() + fare!!.toDouble() - couponFee!!.toDouble()
               zFee.value = "" + s
           }
        }
    }


    fun toAddress() {
        //地址
        UiSwitch.bundleRes(
            repository().context as Activity, AddressListActivity::class.java,
            B().putString("type", "1").ok(), MIntentAction.REQUEST_CODE_ONE
        )
    }

    //获取默认地址
    fun getDefaultAddress() {
        viewModelScope.launch {
            var it = repository().lreq { NetEngine.service.memberAddressListTo(10, 1) }
            if (it.ok()) {
                isShowAddress.value = !(null == it.data?.content || it.data?.content?.size == 0)

                isGetAddress = !isShowAddress.value!!

                if (null != it.data?.content && it.data?.content?.size != 0) {
                    var item = it.data as AddressTo

                    addressId = item.content[0].id
                    realName.value = item.content[0].realName
                    phone.value = item.content[0].phone
                    detailsAddress.value = item.content[0].detailsAddress
                    //去获取车费
                    getCarFee()
                }else{
                    addressId = null
                    realName.value =null
                    phone.value = null
                    detailsAddress.value = null
                }
            }
        }
    }


    //获取车费
    fun getCarFee() {
        viewModelScope.launch {
            var it = repository().lreq { NetEngine.service.getFare(technicianId, addressId) }
            if (it.ok()) {
                fare = it.data?.fare
                fee.value = it.data
                isShowFee.value = true
                setSum()
            }
        }
    }

    //获取时间
    fun getDate() {
        viewModelScope.launch {
            var it = repository().lreq { NetEngine.service.dateList(technicianId) }
            if (it.ok()) {
                dateList.value = it.data
            } else {
                toast(it.msg)
            }
        }
    }


    fun toSubmit(list: ArrayList<PlatInfo>) {


        viewModelScope.launch {
            var it = repository().lreq {
                NetEngine.service.createOrder(
                    technicianId,
                    addressId,
                    Gson().toJson(list),
                    payType,
                    choseTime,
                    couponId,
                    remak,
                    zFee.value
                )
            }
            if (it.ok()) {

                if (payType == "1") {

                    //刷新订单
                    RxBus.get().post(ConfigRx.PLACE_ORDER, "")
                    //跳转到订单详情
                    UiSwitch.bundle(
                       repository().context,
                        ReceivedDetailsActivity::class.java,
                        B().putString("id", it.data?.orderId).ok()
                    )


                    (repository().context as Activity).finish()
                }

                if(payType=="3"){//支付宝

                    orderId=it.data?.orderId
                     val orderInfo = it.data?.alipay
                    CpAlipay.pay(repository().context!!, orderInfo!!)

                }

            }
        }


    }

    //获取服务
    fun getChoseServe(technicianId:String){
        viewModelScope.launch {
            var it= NetEngine.service.platformServiceList(technicianId,null,null)
            if(it.ok()){
                bean.value=it.data

            }

        }
    }

    fun getOrderInfo(id:String?){
        viewModelScope.launch{
            var it=repository().lreq { NetEngine.service.OrderDetail(id) }
            if(it.ok()){
                orderBean.value=it.data
            }

        }
    }

}



